package com.dingli.dao;

import com.dingli.entity.Loan;
import com.dingli.until.JdbcTools;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class LoanDAO {
    private JdbcTools jt = new JdbcTools();

    private Loan ormLoan(Map< String ,Object> map) {
        Loan loan = new Loan();
        if (map.get("id") != null) {
         loan.setId((Integer) map.get("id"));
        }
        if (map.get("uid") != null) {
            loan.setUid((Integer) map.get("Uid"));
        }
        if (map.get("type") != null) {
            loan.setType((Integer) map.get("type"));
        }
        if (map.get("loan_money") != null) {
            loan.setLoan_money((Double) map.get("loan_money"));
        }
        if (map.get("interest_money") != null) {
            loan.setInterest_money((Double) map.get("interest_money"));
        }
        if (map.get("sum_money") != null) {
            loan.setSum_money((Double) map.get("sum_money"));
        }
        if (map.get("create_time") != null) {
            loan.setCreate_time((Date) map.get("create_time"));
        }
        return loan;
    }
    public int insert(Loan loan) {
        String sql = "insert into loan(uid,type,loan_money,interest_money,sum_money,create_time) value(?,?,?,?,?,?)";
        return jt.insert(sql, loan.getUid(),loan.getType(),loan.getLoan_money(),loan.getInterest_money(),loan.getSum_money(),loan.getCreate_time());
    }
    public int deleteById(int uid){
        String sql ="delete from loan where uid=?";
        return jt.delete(sql,uid);
    }
    public   Loan  selectById(int uid) {
       String sql = "select from loan where uid=?";
        List<Map<String ,Object>> list = jt.select(sql, uid);
        if (list.size() == 0) {
            return null;
        }
        return ormLoan(list.get(0));

    }
    public int UpdateById(Loan loan) {
        String sql ="update  loan set uid=?,type=?,loan_money=?,interest_money,sum_money";
        return  jt.update(sql,loan.getUid(),loan.getType(),loan.getLoan_money(),loan.getInterest_money(),loan.getSum_money());
    }
}
